/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package SYSGEST.br.Login;

import SYSGEST.conexao.Utill;
import sysgest.conexao.HibernateUtil;
import Telas.TelaLogin;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author AELSON
 */
public class LoginDAO {
    private Session sessao;
    private Transaction transacao;
    
    public void salvar(Login login){
        setSessao(HibernateUtil.getSessionFactory()
                .openSession());
        setTransacao(getSessao().beginTransaction());
        getSessao().save(login);
        getTransacao().commit();
        getSessao().close();
    }
    
    public Login logar(String log, String senha) {
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        Login login = (Login) getSessao().createCriteria(Login.class)
                .add(Restrictions.eq(("log"), log))
                .add(Restrictions.eq("senha", senha)).uniqueResult();
        return login;

    }
    public boolean autenticar(String login, String senha) {
        setSessao(HibernateUtil.getSessionFactory().openSession());
        Login usuario = new Login();
        senha = Utill.md5(senha);
        usuario = (Login) getSessao().createCriteria(Login.class).add(Restrictions.eq("log", login))
                .add(Restrictions.eq("senha", senha)).uniqueResult();
        getSessao().close();
        if (usuario != null) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * @return the sessao
     */
    public Session getSessao() {
        return sessao;
    }

    /**
     * @param sessao the sessao to set
     */
    public void setSessao(Session sessao) {
        this.sessao = sessao;
    }

    /**
     * @return the transacao
     */
    public Transaction getTransacao() {
        return transacao;
    }

    /**
     * @param transacao the transacao to set
     */
    public void setTransacao(Transaction transacao) {
        this.transacao = transacao;
    }
}
